<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>商家客服</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            background-color: #f8f9fa;
            font-family: Arial, sans-serif;
            height: 100vh;
        }

        .container {
            height: 100vh;
            display: flex;
            flex-direction: column;
            padding: 20px;
            max-width: 1200px;
            margin: 0 auto;
        }

        .header {
            position: relative;
            display: flex;
            justify-content: flex-start;
            align-items: center;
            padding-left: 0px;
            padding-right: 100px;
            margin-bottom: 20px;
        }

        h1 {
            color: #2ecc71;
            font-size: 24px;
            position: absolute;
            left: 50%;
            transform: translateX(-50%);
            white-space: nowrap;
        }

        .back-button {
            padding: 12px 30px; /* 与发送按钮一致 */
            font-size: 14px; /* 统一字号 */
            border: 2px solid #2ecc71;
            margin-left: 20px; /* 对齐对话框边距 */
        }

        .back-button:hover {
            background: #2ecc71;
            color: white;
        }

        .chat-container {
            flex: 1;
            display: flex;
            flex-direction: column;
            background: white;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        .chat-box {
            flex: 1;
            padding: 20px;
            overflow-y: auto;
            display: flex;
            flex-direction: column;
            gap: 15px;
        }

        .input-container {
            padding: 20px;
            background: #f8f9fa;
            border-top: 1px solid #eee;
            display: flex;
            gap: 10px;
        }

        input[type="text"] {
            flex: 1;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 25px;
            outline: none;
        }

        input[type="submit"] {
            background-color: #2ecc71;
            color: #fff;
            border: none;
            padding: 12px 30px;
            border-radius: 25px;
            cursor: pointer;
            transition: background 0.3s;
        }

        input[type="submit"]:hover {
            background-color: #27ae60;
        }

        .message {
            max-width: 80%;
            padding: 15px;
            border-radius: 15px;
            animation: fadeIn 0.3s ease-in;
        }

        .user-message {
            background-color: #2ecc71;
            color: #fff;
            margin-left: auto;
        }

        .bot-message {
            background-color: #f1f1f1;
            color: #333;
            margin-right: auto;
        }

        .loader {
            position: absolute;
            left: 50%;
            top: 80px; /* 根据标题高度调整 */
            transform: translateX(-50%);
            z-index: 10;
            padding: 10px;
            border-radius: 20px;
        }

        .loader::after {
            content: "";
            display: inline-block;
            width: 20px;
            height: 20px;
            border-radius: 50%;
            border: 3px solid #ddd;
            border-top-color: #2ecc71;
            animation: spin 1s infinite linear;
        }

        @keyframes spin {
            to { transform: rotate(360deg); }
        }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(10px); }
            to { opacity: 1; transform: translateY(0); }
        }
    </style>
</head>
<body>
<div class="container">
    <div class="header">
        <button class="back-button" onclick="window.history.back()">← 返回首页</button>
        <h1>商家客服</h1>
    </div>

    <div class="chat-container">
        <div id="chat-box" class="chat-box"></div>
        <div class="input-container">
            <form id="form" style="flex: 1; display: flex; gap: 10px;">
                <input type="text" id="message" name="message" placeholder="输入您的问题...">
                <input type="submit" value="发送">
            </form>
        </div>
    </div>
    <div id="loader" class="loader" style="display: none;"></div>
</div>

<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<script>
    class SpringAIClient {
        constructor(baseURL = "/ai") {
            this.baseURL = baseURL;
        }

        async chat(message) {
            try {
                const response = await fetch(
                    `${this.baseURL}/chat?message=${encodeURIComponent(message)}`,
                    {
                        method: "GET",
                        headers: { "Content-Type": "application/json" }
                    }
                );
                return await response.text();
            } catch (error) {
                return "服务暂时不可用，请稍后再试";
            }
        }
    }

    const aiClient = new SpringAIClient();
    const chatBox = document.getElementById("chat-box");
    const loader = document.getElementById("loader");

    // 初始问候语
    window.addEventListener("DOMContentLoaded", () => {
        addMessage("bot", "您好，我是茶小七，沏沏茶网上茶店的智能客服代表。很高兴为您服务，有什么可以帮助您的吗？");
    });

    document.getElementById("form").addEventListener("submit", async (e) => {
        e.preventDefault();
        const input = document.getElementById("message");
        const message = input.value.trim();
        if (!message) return;

        input.value = "";
        addMessage("user", message);

        loader.style.display = "block";
        try {
            const response = await aiClient.chat(message);
            const parsed = marked.parse(response);
            addMessage("bot", parsed);
        } catch (error) {
            addMessage("bot", "⚠️ 服务请求失败，请稍后重试");
        } finally {
            loader.style.display = "none";
        }
    });

    function addMessage(role, content) {
        const messageDiv = document.createElement("div");
        messageDiv.className = `message ${role}-message`;
        messageDiv.innerHTML = role === "user" ? content : marked.parse(content);
        chatBox.appendChild(messageDiv);
        chatBox.scrollTop = chatBox.scrollHeight;
    }
</script>
</body>
</html>